<?php
$manageResourceStr = Yii::t('site','Manage Resource');
$setupResourceStr = Yii::t('site','Setup resource');
$resourceNameStr = Yii::t('site','Name');
$descriptionStr = Yii::t('site','Description');
$addBasicInfoStr = Yii::t('site','Add basic info');
$resourceAmountStr = Yii::t('site','Resource Amount');
    
$screen = Blyn::app()->getCurrentNode()->getUIManager()->getCurrentScreen();
$app = Blyn::app()->getActiveApp();

$resourceModel = new BSResourceFormModel();

if (!empty($_POST)) {
    if(isset( $_POST['name' . $resourceId]) )
    {
        $resourceModel->id = $resourceId;
        $resourceModel->name = $_POST['name' . $resourceId];
        $resourceModel->description = $_POST['description' . $resourceId];
        $resourceModel->rgSportType = $_POST['rgSportType' . $resourceId];
        $resourceModel->rgArea = $_POST['rgArea' . $resourceId];
        $resourceModel->rgStadium = $_POST['rgStadium' . $resourceId];
        $resourceModel->amount = $_POST['amount' . $resourceId];
    }
    else if($resourceId>0) {
        $resourceModel->id = $resourceId;
        $resourceModel->name = $resource->name;
        $resourceModel->description = $resource->description;
        $resourceModel->amount = $resource->amount;
    }
    
    if ($screen->isAjaxLinkClicked(array('ajaxLinkId' => 'link' . $resourceId))) {
        if ($app instanceof BBookingSports) {
            $app->addResource($resourceModel);
            Yii::app()->end();
        }
    }
}

if($resourceId>0)
{
    $defaultName = $resource->name;
    $defaultDescription = $resource->description;
//        $resourceModel->resourceType = $resource->resourceType;
    $defaultAmount = $resource->amount;
    
    $groups = $resource->getGroups();
    
    if ($groups != null) {
        foreach ($groups as $key => $group) {
            if (array_key_exists($key, $sportTypeList)) {
                $resourceModel->rgSportType = $key;
            } else if (array_key_exists($key, $areaList)) {
                $resourceModel->rgArea = $key;
            } else if (array_key_exists($key, $stadiumList)) {
                $resourceModel->rgStadium = $key;
            }
        }
    }
}
else{
    $defaultName = '';
    $defaultDescription = '';
    $defaultAmount = '1';
}
?>

<script type="text/javascript">
$('body').on('hidden.bs.modal', '.modal', function () {
    $(this).removeData('bs.modal');
});
</script>

<div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
    <h3><?php echo $manageResourceStr; ?></h3>
</div>
<div class="modal-body">

    <div class="tabbable tabs-right" style="margin-top: 15px;"> <!-- Only required for left/right tabs -->
        <ul class="nav nav-tabs" style="margin-bottom: 15px">
            <li class="active"><a href="#tab1<?php echo $resourceId; ?>" data-toggle="tab"><?php echo $addBasicInfoStr; ?></a></li>
            <li><a href="#tab2<?php echo $resourceId; ?>" data-toggle="tab"><?php echo $setupResourceStr; ?></a></li>
        </ul>
        <div class="tab-content">
            <div class="tab-pane active" id="tab1<?php echo $resourceId; ?>" style="min-height: 270px">
                <div class="well">
                    <label><?php echo $resourceNameStr; ?>:</label>
                    <?php echo CHtml::activeTextField($resourceModel, 'name', array('value'=>$defaultName, 'name' => "name" . $resourceId, 'placeholder' => 'Type name...', 'required' => true)); ?>                
                    <label><?php echo $descriptionStr; ?>:</label>                
                    <?php $resourceModel->description = $defaultDescription;
                    echo CHtml::activeTextArea($resourceModel, 'description', array('name' => "description" . $resourceId, 'placeholder' => 'Type description...')); ?>
                </div>
            </div>
            <div class="tab-pane" id="tab2<?php echo $resourceId; ?>" style="min-height: 270px">
                <div class="well">
                    <label><?php echo Yii::t('site', 'Sport Type'); ?>:</label>                    
                    <?php
                    echo CHtml::activeDropDownList($resourceModel, 'rgSportType', $sportTypeList, array('name' => 'rgSportType' . $resourceId));
                    ?>
                    <label><?php echo Yii::t('site', 'Area'); ?>:</label>                    
                    <?php
                    echo CHtml::activeDropDownList($resourceModel, 'rgArea', $areaList, array('name' => 'rgArea' . $resourceId));
                    ?>
                    <label><?php echo Yii::t('site', 'Sport Stadium'); ?>:</label>                    
                    <?php
                    echo CHtml::activeDropDownList($resourceModel, 'rgStadium', $stadiumList, array('name' => 'rgStadium' . $resourceId));
                    ?>
                    <label><?php echo $resourceAmountStr; ?>:</label>
                    <?php echo CHtml::activeTextField($resourceModel, 'amount', array('value'=>$defaultAmount, 'name' => 'amount' . $resourceId, 'placeholder' => 'Type amount...', 'required' => true)); ?>                
                </div>
            </div>
        </div>
    </div>

</div>     
<div class="modal-footer">
    <button class="btn" data-dismiss="modal" aria-hidden="true"><?php echo Yii::t('site','Close'); ?></button>
    <?php
    echo $screen->getAjaxLink(Yii::t('site','Save changes'), array(
        'ajaxLinkId' => 'link' . $resourceId,
        'ajaxContainerId' => 'myModal' . $resourceId,
        'ajaxView' => array('manageResource', 'data' => array('resourceId' => $resourceId)),
        'htmlOptions' => array('class' => 'btn btn-primary'),
        'ajaxOptions' => array('complete' => 'function(){$("#ajax-loaderdiv").hide();$("#myModal'.$resourceId.'").modal("hide");$("#refreshIt").click();}')));
    ?>
</div>



